﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AG.Data;
using System.Drawing;

namespace AGMapEditor.Controls
{
    public partial class AGEMapPanel
    {
        public AGTerrain CurrentTerrain { get; private set; }
        public AGTerrainTile CurrentTerrainTile { get; private set; }
        public AGUnitCategory CurUnitCategory { get; private set; }
        public AGUnit CurUnit { get; private set; }
        public Point CurPos { get; private set; }


        private Size _mapSize;
        public Size MapSize { get { return _mapSize; } }

        public int OffsetPointX;
        public int OffsetPointY;

        public AGMapSwitchPoint SelectedSwitchPoint { get; private set; }

        public void SetOffsetPoint(PointF targetPoint)
        {
            //OffsetPoint = new PointF(targetPoint.X, targetPoint.Y);

            Invalidate();
        }

        public void SetMap(AGMap map)
        {
            _map = map;
            _mapSize.Width = (int)((_map.Col + _map.Row) * TW / 2);
            _mapSize.Height = (int)((_map.Row + _map.Col) * TH / 2);
            _originOffsetY = _map.Col * TH / 2;

            this.Width = _mapSize.Width;
            this.Height = _mapSize.Height;

            Invalidate();
            UpdateObjectList();
            UpdateSwitchPointList();
        }

        public void SetDesignMode(AGMapDesignMode designMode)
        {
            this.MapDesignMode = designMode;
            RaiseState();

            Invalidate();
            this.Focus();
            this.Select();
        }

        public void SetSelectTerrain(AGTerrain terrain, AGTerrainTile tile)
        {
            CurrentTerrain = terrain;
            CurrentTerrainTile = tile;
            RaiseState();
        }

        public void SetSelectUnit(AGUnitCategory category, AGUnit unit)
        {
            CurUnitCategory = category;
            CurUnit = unit;
            RaiseState();
        }

        public void SelectObject(AGMapObject selectedObject)
        {
            this.SelectedObject = selectedObject;
            Invalidate();
            this.Focus();
            this.Select();
        }

        public void DeleteObject(AGMapObject obj)
        {
            for (int index = 0; index < _map.ObjectList.Count; index++)
            {
                AGMapObject mapObj = _map.ObjectList[index];
                if (mapObj.Id == obj.Id)
                {
                    _map.ObjectList.RemoveAt(index);
                    break;
                }
            }

            if (SelectedObject != null && SelectedObject.Id == obj.Id)
            {
                SelectedObject = null;
            } 
            
            UpdateSwitchPointList();
            Invalidate();
        }

        public void RefreshObjects()
        {
            UpdateObjectList();
            Invalidate();
        }

        public void AddSwitchPoint(AGMapSwitchPoint sp)
        {
            _map.SwitchPoints.Add(sp);
            UpdateSwitchPointList();
        }

        public void SelectSwitchPoint(AGMapSwitchPoint sp)
        {
            SelectedSwitchPoint = sp;
        }

        public void UpdateSwitchPoint(AGMapSwitchPoint sp)
        {
        }
    }
}
